//
//  VistaCiudadesPicoYPlaca.m
//  Pico y placa
//
//  Created by Mario Franco on 12/13/12.
//  Copyright (c) 2012 Mario Franco. All rights reserved.
//

#import "VistaCiudadesPicoYPlaca.h"
#import "SessionManager.h"
#import "Ciudad.h"
#import "VistaTipoVehiculosPicoYPlaca.h"
#import "VistaEditarPicoYPlaca.h"

@interface VistaCiudadesPicoYPlaca ()
{
    SessionManager *principal;
    Ciudad *ciudad;
}
@end

@implementation VistaCiudadesPicoYPlaca
@synthesize tabla;
@synthesize bannerView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    principal = [SessionManager darInstancia];
	// Do any additional setup after loading the view.    
}

-(void)viewWillAppear:(BOOL)animated
{
    [self moveBannerViewOffscreen];
    [tabla deselectRowAtIndexPath:[tabla indexPathForSelectedRow] animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [principal.darCiudades count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
    }
    
    Ciudad *c = [[principal darCiudades] objectAtIndex:[indexPath row]];

    cell.textLabel.text = c.nombre;
    cell.textLabel.font=[UIFont fontWithName:@"ArialMT" size:18];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ciudad = [[principal darCiudades] objectAtIndex:[indexPath row]];
    [self performSegueWithIdentifier:@"verTiposVehiculosPicoYPlaca" sender:self];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"verTiposVehiculosPicoYPlaca"])
    {
        VistaTipoVehiculosPicoYPlaca * vista = [segue destinationViewController];
        vista.ciudad = ciudad;
    }        
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)moveBannerViewOffscreen
{
    //Hacer que la subvista1 ocupe el espacio dejado por el bannerView
    CGRect frameOriginalTabla = tabla.frame;
    CGFloat nuevoHeight = self.view.frame.size.height;
    CGRect frameNuevoTabla = frameOriginalTabla;
    frameNuevoTabla.size.height = nuevoHeight;
    
    //Posicionar el bannerView debajo de la subvista1 (offscreen)
    CGRect frameNuevoBanner = bannerView.frame;
    frameNuevoBanner.origin.y = nuevoHeight;
    
    tabla.frame = frameNuevoTabla;
    bannerView.frame = frameNuevoBanner;
}

-(void)moveBannerViewOnscreen
{
    CGRect frameNuevoBanner = bannerView.frame;
    frameNuevoBanner.origin.y = self.view.frame.size.height - frameNuevoBanner.size.height;
    
    CGRect frameOriginalTabla = tabla.frame;
    CGFloat nuevoHeight = self.view.frame.size.height - frameNuevoBanner.size.height;
    CGRect frameNuevoTabla = frameOriginalTabla;
    frameNuevoTabla.size.height = nuevoHeight;
    
    [UIView beginAnimations:@"BannerViewIntro" context:NULL];
    tabla.frame = frameNuevoTabla;
    bannerView.frame = frameNuevoBanner;
    [UIView commitAnimations];
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    [self moveBannerViewOffscreen];
}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    [self moveBannerViewOnscreen];
}

- (void) viewWillDisappear:(BOOL)animated
{
    [bannerView removeFromSuperview];
    bannerView.delegate = nil;
    bannerView = nil;
}

@end
